def plotspec(x, Ts):
fig = figure()
ax1 = fig.add_subplot(211)
ax1.plot(x)
q = fft.fft(x)
ax2 = fig.add_subplot(212)
ax2.plot(fft.fftfreq(len(x), Ts), abs(q))
def arbspec(s, time, Ts):
t = linspace(0.0, time, time/Ts)
x = s(t)
plotspec(x, Ts)
# specdelta.py
time = 2.0
Ts = 1.0/100.0
t = linspace(0, time, time/Ts)
x = zeros(len(t))
x[0] = 1
plotspec(x, Ts)
4.3. Mimic the code in specdelta.m to find the spectrum of the discrete delta function when:
def deltaAt(dt, dx):
def s(t):
x = zeros(len(t))
x[dt] = dx
return x
return s
arbspec( deltaAt( 10,1), 2.0, 1.0/100.0)
arbspec( deltaAt(100,1), 2.0, 1.0/100.0)
arbspec( deltaAt(110,1), 2.0, 1.0/100.0)
arbspec( deltaAt( 1, 20), 2.0, 1.0/100.0)
arbspec( deltaAt( 10, 3), 2.0, 1.0/100.0)
arbspec( deltaAt(100, 0.1), 2.0, 1.0/100.0)
4.4. Mimic the code in specdelta to find the spectrum of a signal containing two delta functions when:
def TwoDeltaAt(dt1, dt2):
def s(t):
x = zeros(len(t))
x[dt1] = 1
x[dt2] = 1
return x
return s
arbspec(TwoDeltaAt(0,-1), 2.0, 1/100.0)
arbspec(TwoDeltaAt(89,-90), 2.0, 1/100.0)
arbspec(TwoDeltaAt(33, 120), 2.0, 1/100.0)
4.5. Mimic the code in specdelta to find the spectrum of a train of equally spaced pulses.
def pulsetrain(skip):
def s(t):
x = zeros(len(t))
i = 0
while i < len(t):
x[i] = 1
x += skip
return x
return s
arbspec(pulsetrain(20), 2.0, 1.0/100.0)
arbspec(pulsetrain(25), 2.0, 1.0/100.0)